package server;

import java.io.OutputStream;

/**
 * http协议工具类
 * 提供200和404的响应头信息
 */
public class HttpProtocolUtil {
    /**
     * 200情况下的响应头
     * @return
     */
    public static String getHttpHeader200(long contentLength){
        //一定要返回固定格式的字符串
        /**
         * Http/1.1 200 OK
         * Cache-Control: private
         * Connection: keep-alive
         * Content-Encoding: gzip
         * Content-Length: 49
         * Content-Type: text/html;charset=utf-8
         * Date: Wed, 09 Dec 2020 01:40:20 GMT
         * Expires: Wed, 09 Dec 2020 01:40:20 GMT
         * Server: BWS/1.0
         * Vary: Accept-Encoding
         */
        String result = "Http/1.1 200 OK \n" +
                "Content-Type: text/html;charset=utf-8 \n "+
                "Content-Length: "+contentLength + " \n" +
                "\r\n";
        return result;
    }

    /**
     * 404情况下的响应头（此处也包含了响应体）
     * @return
     */
    public static String getHttpHeader404(){
        String body = "<h1>404 opts...</h1>";
        String result = "Http/1.1 404 SB \n" +
                "Content-Type: text/html;charset=utf-8 \n "+
                "Content-Length: "+body.getBytes().length + " \n" +
                "\r\n" + body;
        return result;
    }
}
